import { FModule } from "../../Libs/Framework/Support/FModule";
import { stageWidth } from "../../Libs/Framework/Utility/dx/stageWidth";
import { stageHeight } from "../../Libs/Framework/Utility/dx/stageHeight";
import { EventManager } from "../../Libs/Framework/Managers/EventManager";
import { ModuleEvent } from "../../Libs/Framework/Event/ModuleEvent";
import { ModuleNames } from "../../Libs/Framework/Enums/ModuleNames";
import { GameLayer } from "../../Libs/Framework/Enums/GameLayer";

const { ccclass, property } = cc._decorator;
@ccclass
export default class MaskModule extends FModule {
    public static ClassName: string = "MaskModule";

    private graphics: cc.Graphics;
    private hasBlockInput: boolean = false;

    private _alpha: number = 0;
    public get alpha(): number { return this._alpha; }
    public set alpha(value: number) {
        this._alpha = value;
        if (this.graphics != null) {
            this.graphics.clear();
            this.graphics.fillColor = new cc.Color(0, 0, 0, value);
            this.graphics.fillRect(0, 0, stageWidth(), stageHeight());
        }
    }
    protected createMainNode(): cc.Node {
        let n = super.createMainNode();
        this.graphics = n.addComponent(cc.Graphics);
        return n;
    }
    protected showViews(): void {
        super.showViews();
        let alpha = 100;
        let intercept = true;
        if (this.moduleData != null) {
            if (this.moduleData.hasOwnProperty("alpha"))
                alpha = this.moduleData["alpha"];
            if (this.moduleData.hasOwnProperty("intercept"))
                intercept = this.moduleData["intercept"];
        }
        if (alpha == 0) this.alpha = 0;
        else {
            TweenLite.killTweensOf(this);
            TweenLite.to(this, 0.3, { alpha: alpha });
        }
        if (intercept) {
            if (!this.hasBlockInput) {
                this.node.addComponent(cc.BlockInputEvents);
                this.node.setContentSize(stageWidth(), stageHeight());
                this.node.width = stageWidth();
                this.node.height = stageHeight();
                this.hasBlockInput = true;
            }
        } else {
            if (this.hasBlockInput)
                this.node.removeComponent(cc.BlockInputEvents);
            this.hasBlockInput = false;
        }
    }
    public hide(data?: object): void {
        if (this.node && this.node.parent && this.node.parent.parent) {
            let layers: any = this.node.parent.parent;
            let popupLayer: cc.Node = layers.getChildByName("PopupLayer");
            let windowLayer: cc.Node = layers.getChildByName("WindowLayer");
            if (popupLayer || windowLayer) {
                if (windowLayer.childrenCount > 0) {
                    EventManager.dispatchModuleEventWith(ModuleEvent.SHOW_MODULE, ModuleNames.Mask, GameLayer.WindowMask);
                    return;
                }
                if (popupLayer.childrenCount > 0) {
                    EventManager.dispatchModuleEventWith(ModuleEvent.SHOW_MODULE, ModuleNames.Mask, GameLayer.PopupMask);
                    return;
                }
            }
        }
        super.hide(data);
    }
}
